<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>媒体查询示例</title>
    <style>
        /* 基础样式 */
        :root {
            --primary-color: #3498db;
            --text-color: #333;
            --bg-color: #f8f9fa;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
            line-height: 1.6;
            color: var(--text-color);
            background-color: var(--bg-color);
        }

        .container {
            width: 100%;
            padding: 15px;
            margin: 0 auto;
        }

        .header {
            background: var(--primary-color);
            color: white;
            padding: 1rem;
            text-align: center;
        }

        .nav {
            background: white;
            padding: 1rem;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .nav-list {
            list-style: none;
            display: none; /* 移动端默认隐藏 */
        }

        .nav-item {
            padding: 0.5rem 0;
            border-bottom: 1px solid #eee;
        }

        .nav-toggle {
            display: block;
            background: none;
            border: none;
            font-size: 1.5rem;
            cursor: pointer;
        }

        .content {
            display: grid;
            gap: 1rem;
            padding: 1rem 0;
        }

        .card {
            background: white;
            padding: 1rem;
            border-radius: 4px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        /* 平板设备 (768px及以上) */
        @media (min-width: 768px) {
            .container {
                max-width: 720px;
            }

            .nav-toggle {
                display: none;
            }

            .nav-list {
                display: flex;
                gap: 2rem;
            }

            .nav-item {
                border: none;
            }

            .content {
                grid-template-columns: repeat(2, 1fr);
            }
        }

        /* 桌面设备 (1024px及以上) */
        @media (min-width: 1024px) {
            .container {
                max-width: 960px;
            }

            .content {
                grid-template-columns: repeat(3, 1fr);
            }
        }

        /* 打印样式 */
        @media print {
            .nav,
            .nav-toggle {
                display: none;
            }

            .card {
                break-inside: avoid;
                box-shadow: none;
                border: 1px solid #ddd;
            }
        }
    </style>
</head>
<body>
    <header class="header">
        <div class="container">
            <h1>媒体查询示例</h1>
        </div>
    </header>

    <nav class="nav">
        <div class="container">
            <button class="nav-toggle">☰</button>
            <ul class="nav-list">
                <li class="nav-item"><a href="#home">首页</a></li>
                <li class="nav-item"><a href="#about">关于</a></li>
                <li class="nav-item"><a href="#services">服务</a></li>
                <li class="nav-item"><a href="#contact">联系</a></li>
            </ul>
        </div>
    </nav>

    <main class="container">
        <div class="content">
            <article class="card">
                <h2>响应式设计</h2>
                <p>响应式网页设计（Responsive Web Design）是一种网页设计方法，使得一个网站能够兼容多个终端。</p>
            </article>
            <article class="card">
                <h2>媒体查询</h2>
                <p>媒体查询是响应式设计的核心技术之一，它允许我们根据设备特性应用不同的样式。</p>
            </article>
            <article class="card">
                <h2>移动优先</h2>
                <p>移动优先设计意味着我们首先为移动设备设计网站，然后逐步扩展到更大的屏幕。</p>
            </article>
        </div>
    </main>

    <script>
        // 移动端菜单切换
        const navToggle = document.querySelector('.nav-toggle');
        const navList = document.querySelector('.nav-list');

        navToggle.addEventListener('click', () => {
            navList.style.display = navList.style.display === 'block' ? 'none' : 'block';
        });

        // 监听窗口大小变化，处理导航显示
        window.addEventListener('resize', () => {
            if (window.innerWidth >= 768) {
                navList.style.display = 'flex';
            } else {
                navList.style.display = 'none';
            }
        });
    </script>
</body>
</html>
